package com.ty.service.imple;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ty.dao.BaseDaoI;
import com.ty.entity.SysAdmin;
import com.ty.entity.SysRole;
import com.ty.entity.SysUserRole;
import com.ty.service.UserRoleService;

@Service
public class UserRoleServiceImple implements UserRoleService {

	@Autowired
	private BaseDaoI<SysUserRole> dao;
	
	@Override
	public boolean batchUserRole(Long userId, Long roleId[]) {
		try {
			//删除关联操作
			String hql="delete from SysUserRole o where o.sysAdmin.id="+userId;
			Query query= dao.getCurrentSession().createQuery(hql);
			query.executeUpdate();
			
			if(roleId==null) {
				return true;
			}
			
			for (int i = 0; i < roleId.length; i++) {
				SysUserRole sysUserRole=new SysUserRole();
				//保存角色
				SysRole sysRole=new SysRole();
				sysRole.setId(roleId[i]);
				sysUserRole.setSysRole(sysRole);
				//保存的用户
				SysAdmin sysAdmin=new SysAdmin();
				sysAdmin.setId(userId);
				sysUserRole.setSysAdmin(sysAdmin);
				dao.save(sysUserRole);
			}
			return true;
		}catch(Exception e) {
			e.printStackTrace();
			return false;
		}
	
	}

}
